package com.chagee.store.operation.server.utils;

import com.alibaba.ttl.TransmittableThreadLocal;
import net.sf.jsqlparser.statement.select.PlainSelect;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class SqlThreadLocalUtil {
    private static final ThreadLocal<Map<String, PlainSelect>> SQL_THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static Map<String,PlainSelect> get() {
		if (Objects.isNull(SQL_THREAD_LOCAL.get())){
			synchronized (UserUtil.class){
				if (Objects.isNull(SQL_THREAD_LOCAL.get())){

					SQL_THREAD_LOCAL.set(new ConcurrentHashMap<>());
				}
			}
		}
		return SQL_THREAD_LOCAL.get();
    }

    public static void set(Map<String,PlainSelect> map){
        SQL_THREAD_LOCAL.set(map);
    }
    public static void remove(){
        SQL_THREAD_LOCAL.remove();
    }
}
